package com.huabo.fxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huabo.fxgl.entity.Find;
import com.huabo.fxgl.entity.YyCompany;
import com.huabo.fxgl.mapper.YyCompanyMapper;
import com.huabo.fxgl.mapper.YyPriceMapper;
import com.huabo.fxgl.service.IYyCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiYe
 * @since 2022-08-02
 */
@Service
public class YyCompanyServiceImpl extends ServiceImpl<YyCompanyMapper, YyCompany> implements IYyCompanyService {

    @Autowired
    private YyPriceMapper yyPriceMapper;

    @Override
    public IPage<YyCompany> findCompanyListByTeamid(IPage page, Find find, Integer teamid) {
        QueryWrapper<YyCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teamid", teamid);
        if (find != null && find.getStaffid() != null && find.getStaffid().length() > 0) {
            queryWrapper.eq("STAFFID", find.getStaffid());
        }
        if (find != null && find.getFxtype() != null && find.getFxtype().length() > 0) {
            queryWrapper.eq("fxtype", find.getFxtype());
        }
        if (find != null && find.getCompanyname() != null && find.getCompanyname().length() > 0) {
            queryWrapper.like("COMPANYNAME", find.getCompanyname());
        }
        queryWrapper.orderByAsc("companyid");
        IPage<YyCompany> pageBean = page(page, queryWrapper);
        for (YyCompany company : pageBean.getRecords()) {
            if (company.getReport()!=null) {
                company.setPriceList(yyPriceMapper.selectListByIds(company.getReport().getPriceid()));
            }
        }
        return pageBean;
    }

    @Override
    public IPage<YyCompany> findCompanyList(IPage page, Find find, Integer orgid, Integer staffid) {
        QueryWrapper<YyCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ORGID", orgid);
        queryWrapper.eq("staffid", staffid);
//        String sql="select * from TBL_YY_COMPANY where ORGID="+orgid+" and staffid="+userid;
        if (find != null && find.getFxtype() != null && find.getFxtype().length() > 0) {
            queryWrapper.eq("fxtype", find.getFxtype());
        }
        if (find != null && find.getCompanyname() != null && find.getCompanyname().length() > 0) {
            queryWrapper.like("COMPANYNAME", find.getCompanyname());
        }
        queryWrapper.orderByAsc("companyid");
        IPage<YyCompany> pageBean = page(page, queryWrapper);
        for (YyCompany company : pageBean.getRecords()) {
            if (company.getReport()!=null && StringUtils.isNotBlank(company.getReport().getPriceid())) {
                company.setPriceList(yyPriceMapper.selectListByIds(company.getReport().getPriceid()));
            }
        }
        return pageBean;
    }
}
